package demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class pcb_demo extends JFrame {
    //就绪队列，位于内存，一切资源就绪，仅缺少CPU资源
    ArrayList< pcb> pcb_jiuxu = new ArrayList< pcb >();
    //挂起队列，位于外存，手动选择 就绪队列 中的进程挂起，系统将回收内存以节省资源
    ArrayList< pcb> pcb_guaqi = new ArrayList< pcb >();
    //终止队列,已经完成，剥夺CPU资源
    ArrayList< pcb > pcb_zhongzhi = new ArrayList< pcb >();
    //后备队列，位于外存，作业调度将选择作业进入内存
    ArrayList< pcb > pcb_houbei = new ArrayList< pcb >();

    //    时间片算法
    private void jButton7MouseClicked(MouseEvent evt) {
//        获取时间片下拉框的文本信息
        String substring = "";
//        判空操作
//        截取下拉框中的数字
        if (!"".equals(jComboBox3.getSelectedItem().toString())) {
            for (int i = 0; i <jComboBox3.getSelectedItem().toString().length(); i++) {
                if (jComboBox3.getSelectedItem().toString().charAt(i)>=48&&jComboBox3.getSelectedItem().toString().charAt(i)<=57){
                    substring+=jComboBox3.getSelectedItem().toString().charAt(i);
                }
            }
        }
//        获得下拉框中的数字
        int time_stamp=Integer.parseInt(substring);

        while(time_stamp!=0) {

            time_stamp--;
            pcb p_temp=pcb_jiuxu.get(0);
            jTextField7.setText(p_temp.name);
            jTextField8.setText(p_temp.time);
            jTextField9.setText(p_temp.rank);

            int time_to_int=Integer.parseInt(p_temp.time);
            int rank_to_int=Integer.parseInt(p_temp.rank);

            //使用pcb_ptemp3表示删掉旧的进程pcb
            pcb_jiuxu.remove(0);

            time_to_int--;
            rank_to_int--;


            String temp_name=p_temp.name;
            String temp_time=String.valueOf(time_to_int);
            String temp_rank=String.valueOf(rank_to_int);
            pcb p_add_temp=new pcb(temp_name,temp_time,temp_rank);
            if(time_to_int!=0) {
                pcb_jiuxu.add(p_add_temp);
            }
            else {
                pcb_zhongzhi.add(p_add_temp);
            }

            new_genxin();

        }

    }

    //    静态优，按照优先级调入就绪队列
    private void jButton9MouseClicked(MouseEvent evt) {
//        获取内存分配块数
        int dao_num=Integer.parseInt(jComboBox4.getSelectedItem().toString());
        pcb_sort(pcb_houbei);
        while(dao_num!=0) {
            dao_num--;
            if(pcb_houbei.size()>0) {
                pcb_jiuxu.add(pcb_houbei.get(0));
                pcb_houbei.remove(0);
            }
            else break;
        }
        new_genxin();
    }

    //按照优先级对pcb的数组排序
    public  ArrayList<pcb> pcb_sort( ArrayList<pcb> par) {
        int p_size=par.size();
        for(int i=0;i<p_size;i++) {
            for(int j=0;j<i;j++) {
                pcb a=par.get(i);
                pcb b=par.get(j);

                pcb temp;
                int rank1=Integer.parseInt(a.rank);
                int rank2=Integer.parseInt(b.rank);

                if(rank1>rank2) {
                    par.set(i, b);
                    par.set(j, a);
                }
            }
        }
        return par;
    }
//    实时更新队列信息
    public void new_genxin() {

        int p_size=pcb_jiuxu.size();

        String []str=new String[p_size];


        for(int i=0;i<p_size;i++) {
            str[i]=get_pcb(pcb_jiuxu.get(i));
        }

        jList6.setListData(str);
        //更新就绪队列的jlist6

        int p_size1=pcb_houbei.size();
        String []str1=new String[p_size1];
        for(int i=0;i<p_size1;i++) {
            str1[i]=get_pcb(pcb_houbei.get(i));
        }

        jList5.setListData(str1);
        //更新后备队列的jlist5

        int p_size2=pcb_guaqi.size();
        String []str2=new String[p_size2];
        for(int i=0;i<p_size2;i++) {
            str2[i]=get_pcb(pcb_guaqi.get(i));
        }
        jList8.setListData(str2);
        //更新挂起队列


        int p_size3=pcb_zhongzhi.size();
        String []str3=new String[p_size3];
        for(int i=0;i<p_size3;i++) {
            str3[i]=get_pcb(pcb_zhongzhi.get(i));
        }
        jList7.setListData(str3);

        //更新终止队列


    }

//    初始化面板信息
    private void initComponents() {
        jScrollPane1 = new  JScrollPane();
        
        jList1 = new  JList<>();
        jLabel4 = new  JLabel();
        jLabel5 = new  JLabel();
        jLabel6 = new  JLabel();
        jScrollPane2 = new  JScrollPane();
        jList2 = new  JList<>();
        jScrollPane3 = new  JScrollPane();
        jList3 = new  JList<>();
        jScrollPane4 = new  JScrollPane();
        jList4 = new  JList<>();
        jLabel7 = new  JLabel();
        jLabel8 = new  JLabel();
        jLabel9 = new  JLabel();
        jButton5 = new  JButton();
        jComboBox5 = new  JComboBox<>();
        jButton8 = new  JButton();
        jLabel15 = new  JLabel();
        jLabel16 = new  JLabel();
        jScrollPane6 = new  JScrollPane();
        jList6 = new  JList<>();
        jButton3 = new  JButton();
        jScrollPane8 = new  JScrollPane();
        jList8 = new  JList<>();
        jButton4 = new  JButton();
        jButton6 = new  JButton();
        jButton7 = new  JButton();
        jTextField10 = new  JTextField();
        jLabel17 = new  JLabel();
        jLabel18 = new  JLabel();
        jLabel19 = new  JLabel();
        jScrollPane7 = new  JScrollPane();
        jList7 = new  JList<>();
        jLabel10 = new  JLabel();
        jScrollPane5 = new  JScrollPane();
        jList5 = new  JList<>();
        jLabel11 = new  JLabel();
        jLabel12 = new  JLabel();
        jLabel13 = new  JLabel();
        jTextField7 = new  JTextField();
        jTextField8 = new  JTextField();
        jTextField9 = new  JTextField();
        jLabel14 = new  JLabel();
        jComboBox4 = new  JComboBox<>();
        jButton2 = new  JButton();
        jLabel1 = new  JLabel();
        jLabel2 = new  JLabel();
        jLabel3 = new  JLabel();
        jTextField1 = new  JTextField();
        jTextField4 = new  JTextField();
        jComboBox1 = new  JComboBox<>();
        jTextField2 = new  JTextField();
        jTextField5 = new  JTextField();
        jComboBox2 = new  JComboBox<>();
        jTextField3 = new  JTextField();
        jTextField6 = new  JTextField();
        jComboBox3 = new  JComboBox<>();
        jButton1 = new  JButton();
        jButton9 = new  JButton();
        jTextField11 = new JTextField();
        jTextField12=new JTextField();
        jTextField13=new JTextField();


        jList1.setModel(new AbstractListModel<String>() {

            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public String getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel4.setText("jLabel4");

        jLabel5.setText("jLabel5");

        jLabel6.setText("jLabel6");

        jList2.setModel(new AbstractListModel<String>() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public String getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        jList3.setModel(new AbstractListModel<String>() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public String getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList3);

        jList4.setModel(new AbstractListModel<String>() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public String getElementAt(int i) { return strings[i]; }
        });
        jScrollPane4.setViewportView(jList4);

        jLabel7.setText("进程名");

        jLabel8.setText("运行时间");

        jLabel9.setText("优先级");

        jButton5.setText("jButton5");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });

        jComboBox5.setModel(new  DefaultComboBoxModel<>(new String[] { "1，2，3，4" }));

        jButton8.setText("运行");
        jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton8MouseClicked(evt);
            }
        });

        jLabel15.setText("jLabel15");

        jLabel16.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jLabel16.setText("时间片大小");

        setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(51, 50, 220));

        jList6.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jScrollPane6.setViewportView(jList6);

        jButton3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jButton3.setText("优先权调度算法");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jList8.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jScrollPane8.setViewportView(jList8);

        jButton4.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jButton4.setText("挂起");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        jButton6.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jButton6.setText("解挂");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton6MouseClicked(evt);
            }
        });
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton7.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        jButton7.setText("时间片轮转算法");
        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton7MouseClicked(evt);
            }
        });
        jButton7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        /*
        jTextField10.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField10ActionPerformed(evt);
            }
        });*/

        jLabel17.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel17.setText("就绪队列");

        jLabel18.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel18.setText("挂起队列");

        jLabel19.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel19.setText("终止队列");

        jList7.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jScrollPane7.setViewportView(jList7);

        jLabel10.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel10.setText("后备队列");

        jList5.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jScrollPane5.setViewportView(jList5);

        jLabel11.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel11.setText("进程名");

        jLabel12.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel12.setText("时间");

        jLabel13.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel13.setText("优先级");

        jTextField7.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N

        jTextField8.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N

        jTextField9.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N

        jLabel14.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel14.setText("选择道数");

        jComboBox4.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jComboBox4.setModel(new  DefaultComboBoxModel<>(new String[] { "0","1", "2", "3", "4", "5", "6" }));
        jComboBox4.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox4ItemStateChanged(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jButton2.setText("时间片运转");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel1.setText("进程名");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel2.setText("运行时间");

        jLabel3.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel3.setText("优先权");

        jTextField1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField1.setText("a");

        jTextField4.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField4.setText("6");

        jComboBox1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jComboBox1.setModel(new  DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5", "6" }));
        jComboBox1.setSelectedIndex(3);
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jTextField2.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField2.setText("b");

        jTextField5.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField5.setText("5");

        jTextField11.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField11.setText("1");

        jTextField12.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField12.setText("2");
        /*
        jComboBox2.setModel(new  DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5", "6" }));
        jComboBox2.setSelectedIndex(5);
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });*/
        jComboBox3.setModel(new  DefaultComboBoxModel<>(new String[] { "时间片大小：1", "时间片大小：2", "时间片大小：3", "时间片大小：4", "时间片大小：5", "时间片大小：6" }));
        jComboBox3.setSelectedIndex(5);
        jComboBox3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jTextField3.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField3.setText("c");
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jTextField6.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField6.setText("4");

        jTextField13.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jTextField13.setText("3");
        /*
        jComboBox3.setModel(new  DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5", "6" }));
        jComboBox3.setSelectedIndex(4);*/

        jButton1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jButton1.setText("添加进程");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton9.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jButton9.setText("优先权调度");
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton9MouseClicked(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel17,  GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(45, 45, 45)
                                                                .addComponent(jButton4, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                                                .addComponent(jScrollPane8, GroupLayout.Alignment.LEADING)
                                                                                .addGroup(layout.createSequentialGroup()
                                                                                        .addComponent(jLabel18, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                                                                                        .addGap(114, 114, 114)
                                                                                        .addComponent(jButton6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                                                        .addGroup(layout.createSequentialGroup()
                                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                                                                                .addComponent(jScrollPane6, GroupLayout.Alignment.LEADING)
                                                                                                .addGroup(layout.createSequentialGroup()
                                                                                                        .addComponent(jButton7, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
                                                                                                        .addGap(45, 45, 45)
                                                                                                        .addComponent(jComboBox3, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE)))
                                                                                        .addComponent(jButton1))
                                                                                .addGap(8, 8, 8)))
                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                        .addGroup(layout.createSequentialGroup()
                                                                                .addGap(249, 249, 249)
                                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(jLabel10,  GroupLayout.PREFERRED_SIZE, 162,GroupLayout.PREFERRED_SIZE)
                                                                                        .addGroup(layout.createSequentialGroup()
                                                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                                                        .addGroup(layout.createSequentialGroup()
                                                                                                                .addGap(113, 113, 113)
                                                                                                                .addComponent(jLabel12, GroupLayout.PREFERRED_SIZE, 124,GroupLayout.PREFERRED_SIZE)
                                                                                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
                                                                                                        .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                                                                                .addComponent(jTextField8, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
                                                                                                                .addGap(19, 19, 19)))
                                                                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                                                                                        .addComponent(jLabel13)
                                                                                                        .addComponent(jTextField9, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 105,  GroupLayout.PREFERRED_SIZE)))))
                                                                        .addGroup(layout.createSequentialGroup()
                                                                                .addGap(230, 230, 230)
                                                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(jScrollPane7, GroupLayout.PREFERRED_SIZE, 384, GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(jLabel19,  GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))))))
                                                .addGap(41, 41, Short.MAX_VALUE))
                                        .addGroup( GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(jTextField3,  GroupLayout.PREFERRED_SIZE, 92,  GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jTextField1,  GroupLayout.PREFERRED_SIZE, 92,  GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(jTextField2,  GroupLayout.PREFERRED_SIZE, 92,  GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(jLabel1,  GroupLayout.PREFERRED_SIZE, 94,  GroupLayout.PREFERRED_SIZE))
                                                                .addGap(63, 63, 63)
                                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                                                        .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING, false)
                                                                                .addGroup(layout.createSequentialGroup()
                                                                                        .addComponent(jTextField6,  GroupLayout.PREFERRED_SIZE, 82,  GroupLayout.PREFERRED_SIZE)
                                                                                        .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                                                                                        .addComponent(jTextField13,  GroupLayout.PREFERRED_SIZE, 74,  GroupLayout.PREFERRED_SIZE))
                                                                                .addGroup(layout.createSequentialGroup()
                                                                                        .addComponent(jTextField5,  GroupLayout.PREFERRED_SIZE, 82,  GroupLayout.PREFERRED_SIZE)
                                                                                        .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED,  GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                        .addComponent(jTextField12,  GroupLayout.PREFERRED_SIZE, 74,  GroupLayout.PREFERRED_SIZE))
                                                                                .addGroup(layout.createSequentialGroup()
                                                                                        .addComponent(jTextField4,  GroupLayout.PREFERRED_SIZE, 82,  GroupLayout.PREFERRED_SIZE)
                                                                                        .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED,  GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                                        .addComponent(jTextField11,  GroupLayout.PREFERRED_SIZE, 74,  GroupLayout.PREFERRED_SIZE)))
                                                                        .addGroup(layout.createSequentialGroup()
                                                                                .addComponent(jLabel2)
                                                                                .addGap(77, 77, 77)
                                                                                .addComponent(jLabel3,  GroupLayout.PREFERRED_SIZE, 98,  GroupLayout.PREFERRED_SIZE)))))
                                                .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED,  GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                                        .addComponent(jScrollPane5,  GroupLayout.PREFERRED_SIZE, 334,  GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jButton2,  GroupLayout.PREFERRED_SIZE, 171,  GroupLayout.PREFERRED_SIZE)
                                                                .addGap(27, 27, 27)
                                                                .addComponent(jButton9))
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                                                        .addComponent(jLabel11)
                                                                        .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                                                                .addComponent(jTextField7,  GroupLayout.Alignment.TRAILING,  GroupLayout.PREFERRED_SIZE, 114,  GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(jLabel14,  GroupLayout.PREFERRED_SIZE, 108,  GroupLayout.PREFERRED_SIZE)))
                                                                .addGap(18, 18, 18)
                                                                .addComponent(jComboBox4,  GroupLayout.PREFERRED_SIZE, 93,  GroupLayout.PREFERRED_SIZE)))
                                                .addGap(74, 74, 74))))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(81, 81, 81)
                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1,  GroupLayout.PREFERRED_SIZE, 28,  GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3)
                                        .addComponent(jLabel10,  GroupLayout.PREFERRED_SIZE, 28,  GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(40, 40, 40)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jTextField1,  GroupLayout.PREFERRED_SIZE, 42,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField4,  GroupLayout.PREFERRED_SIZE, 42,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField11,  GroupLayout.PREFERRED_SIZE, 42,  GroupLayout.PREFERRED_SIZE))
                                                .addGap(39, 39, 39)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jTextField2,  GroupLayout.PREFERRED_SIZE, 45,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField5,  GroupLayout.PREFERRED_SIZE, 43,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField12,  GroupLayout.PREFERRED_SIZE, 43,  GroupLayout.PREFERRED_SIZE))
                                                .addGap(39, 39, 39)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jTextField3,  GroupLayout.PREFERRED_SIZE, 38,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField6,  GroupLayout.PREFERRED_SIZE, 38,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField13,  GroupLayout.PREFERRED_SIZE, 38,  GroupLayout.PREFERRED_SIZE)))
                                        .addGroup( GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jScrollPane5,  GroupLayout.PREFERRED_SIZE, 205,  GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap( LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                        .addComponent(jButton1)
                                        .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton9,  GroupLayout.PREFERRED_SIZE, 37,  GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jButton2)))
                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(56, 56, 56)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel11,  GroupLayout.PREFERRED_SIZE, 30,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLabel12)
                                                        .addComponent(jLabel13,  GroupLayout.PREFERRED_SIZE, 30,  GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED,  GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jTextField7,  GroupLayout.PREFERRED_SIZE, 55,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField8,  GroupLayout.PREFERRED_SIZE, 57,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jTextField9,  GroupLayout.PREFERRED_SIZE, 57,  GroupLayout.PREFERRED_SIZE)))
                                        .addGroup( GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGap(8, 8, 8)
                                                .addComponent(jLabel17,  GroupLayout.PREFERRED_SIZE, 28,  GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jScrollPane6,  GroupLayout.PREFERRED_SIZE, 151,  GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(35, 35, 35)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jButton7,  GroupLayout.PREFERRED_SIZE, 41,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jComboBox3,  GroupLayout.PREFERRED_SIZE,  GroupLayout.DEFAULT_SIZE,  GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(26, 26, 26)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jComboBox4,  GroupLayout.PREFERRED_SIZE,  GroupLayout.DEFAULT_SIZE,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLabel14))))
                                .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED,  GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING)
                                        .addGroup( GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jButton3,  GroupLayout.PREFERRED_SIZE, 45,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jButton4))
                                                .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup( GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel18,  GroupLayout.PREFERRED_SIZE, 32,  GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jButton6))
                                                .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jScrollPane8,  GroupLayout.PREFERRED_SIZE,  GroupLayout.DEFAULT_SIZE,  GroupLayout.PREFERRED_SIZE))
                                        .addGroup( GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jLabel19)
                                                .addGap(18, 18, 18)
                                                .addComponent(jScrollPane7,  GroupLayout.PREFERRED_SIZE,  GroupLayout.DEFAULT_SIZE,  GroupLayout.PREFERRED_SIZE)))
                                .addGap(50, 50, 50))
        );
        getContentPane().setBackground(new Color(50,200,100));
        pack();
    }// </editor-fold>
    public pcb_demo() {
        initComponents();
    }

//    添加进程对象
    private void jButton1MouseClicked(MouseEvent evt) {
        String name=jTextField1.getText();
        String time=jTextField4.getText();
        String rank=jTextField11.getText();

        String name1=jTextField2.getText();
        String time1=jTextField5.getText();
        String rank1=jTextField12.getText();

        String name2=jTextField3.getText();
        String time2=jTextField6.getText();
        String substring = "";
//        判空操作
        if (!"".equals(jTextField13.getText())) {
            for (int i = 0; i <jTextField13.getText().length(); i++) {
                if (jTextField13.getText().charAt(i)>=48&&jTextField13.getText().charAt(i)<=57){
                    substring+=jTextField13.getText().charAt(i);
                }
            }
        }
        System.out.println("测试数据"+jTextField13.getText());
        String rank2=substring;

        pcb pcb_1=new pcb(name,time,rank);
        pcb pcb_2=new pcb(name1,time1,rank1);
        pcb pcb_3=new pcb(name2,time2,rank2);

        pcb_houbei.add(pcb_1);
        pcb_houbei.add(pcb_2);
        pcb_houbei.add(pcb_3);
        int size=pcb_houbei.size();
        String []str=new String[size];
        for(int i=0;i<size;i++) {
            str[i]=get_pcb(pcb_houbei.get(i));
        }
        jList5.setListData(str);
    }
//    移动到就绪队列尾部
    private void jButton2MouseClicked(MouseEvent evt) {
        int dao_num=Integer.parseInt(jComboBox4.getSelectedItem().toString());

        while(dao_num!=0) {
            dao_num--;
            if(pcb_houbei.size()>0) {
                pcb_jiuxu.add(pcb_houbei.get(0));
                pcb_houbei.remove(0);
            }
            else break;

        }

        new_genxin();

    }
//    选择处理方法：获得处理机
    private void jButton3MouseClicked(MouseEvent evt) {
        pcb_sort(pcb_jiuxu);//对数组按照指定的办法进行排序

        pcb p_temp=pcb_jiuxu.get(0);
        pcb_jiuxu.remove(0);
        String name=p_temp.name;
        String rank=p_temp.rank;
        String time=p_temp.time;

        int time_to_int=Integer.parseInt(time);
        int rank_to_int=Integer.parseInt(rank);

        time_to_int--;
        rank_to_int--;
//        还有运行时间,获得处理机
        if(time_to_int>0)
        {
            pcb p_temp2=new pcb(name,String.valueOf(time_to_int),String.valueOf( rank_to_int));
            pcb_jiuxu.add( p_temp2);
            int p_size=pcb_jiuxu.size();
            String []str=new String[p_size];

            for(int i=0;i<p_size;i++) {
                str[i]=get_pcb(pcb_jiuxu.get(i));
            }

            new_genxin();
        }
//        没有运行时间,移入终止队列
        else {
            //删掉旧的pcb
            pcb p_temp2=new pcb(name,String.valueOf(time_to_int),String.valueOf(rank_to_int));
            pcb_jiuxu.remove(p_temp2);
            pcb_zhongzhi.add(p_temp2);
            new_genxin();

        }


        //设置显示
        jTextField7.setText(name);
        jTextField8.setText(time);
        jTextField9.setText(rank);

        int select_index=jComboBox4.getSelectedIndex();

//        作业调度逻辑
        while(pcb_jiuxu.size()<select_index&&pcb_houbei.size()>0)
        {
            //如果就绪队列里面的进程数小于规定的道数，且后备队列有进程，则将后备队列调入
            pcb_jiuxu.add(pcb_houbei.get(0));

            pcb_houbei.remove(0);
        }
    }
//    移入挂起队列
    private void jButton4MouseClicked(MouseEvent evt) {
        //先找到要挂起的是哪一个
        int select_index=jList6.getSelectedIndex();
        pcb temp=pcb_jiuxu.get(select_index);
        pcb_jiuxu.remove(select_index);
        //删除在就绪队列中所选中的编号
        pcb_guaqi.add(temp);

        //调用这个函数更新界面的显示内容
        new_genxin();

    }
//    解挂
    private void jButton5MouseClicked(MouseEvent evt) {
        //找到要解挂的进程的编号
        int select_index=jList8.getSelectedIndex();
        pcb temp=pcb_guaqi.get(select_index);
        pcb_guaqi.remove(select_index);
        pcb_jiuxu.add(0, temp);


        //调用这个函数更新界面的显示内容
        new_genxin();
    }
//    根据磁盘道数决策是否处理新进程
    private void jComboBox4ItemStateChanged(ItemEvent evt) {
        int select_index=Integer.parseInt(jComboBox4.getSelectedItem().toString());

        int num_of_jiuxu=pcb_jiuxu.size();
        while(pcb_jiuxu.size()<select_index&&pcb_houbei.size()>0) {
            //如果就绪队列里面的进程数小于规定的道数，且后备队列有进程，则将后备队列调入
            pcb_jiuxu.add(pcb_houbei.get(0));
            pcb_houbei.remove(0);
        }
    }

    private void jButton6MouseClicked(MouseEvent evt) {
        int select_index=jList8.getSelectedIndex();
        pcb temp=pcb_guaqi.get(select_index);
        pcb_guaqi.remove(select_index);
        pcb_jiuxu.add(temp);

        //调用这个函数更新界面的显示内容
        new_genxin();

    }

    public String get_pcb(pcb p) {
        String str="进程名";
        str+=p.name;
        str+=" ";
        str+="运行时间";
        str+=p.time;
        str+=" ";
        str+="优先级:";
        str+=p.rank;
        return str;

    }

    private void jTextField10ActionPerformed(ActionEvent evt) {
    }

    private void jButton7ActionPerformed(ActionEvent evt) {}

    private void jButton6ActionPerformed(ActionEvent evt) {

    }

    private void jButton8MouseClicked(MouseEvent evt) {


    }

    private void jTextField3ActionPerformed(ActionEvent evt) {
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
    }

    private void jComboBox2ActionPerformed(ActionEvent evt) {
    }

    private void jButton1ActionPerformed(ActionEvent evt) {}

    private void jComboBox1ActionPerformed(ActionEvent evt) {

    }

    private void jLabel1MouseClicked(MouseEvent evt) {
    }

//启动
    public static void main(String args[]) {
        try {
            for ( UIManager.LookAndFeelInfo info :  UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(pcb_demo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(pcb_demo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(pcb_demo.class.getName()).log(Level.SEVERE, null, ex);
        } catch ( UnsupportedLookAndFeelException ex) {
            Logger.getLogger(pcb_demo.class.getName()).log(Level.SEVERE, null, ex);
        }
//        启动面板
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new pcb_demo().setVisible(true);
            }
        });
    }

    // 已声明的遍历

    private  JButton jButton1;
    private  JButton jButton2;
    private  JButton jButton3;
    private  JButton jButton4;
    private  JButton jButton5;
    private  JButton jButton6;
    private  JButton jButton7;
    private  JButton jButton8;
    private  JButton jButton9;
    private  JComboBox<String> jComboBox1;
    private  JComboBox<String> jComboBox2;
    private  JComboBox<String> jComboBox3;
    private  JComboBox<String> jComboBox4;
    private  JComboBox<String> jComboBox5;
    private  JLabel jLabel1;
    private  JLabel jLabel10;
    private  JLabel jLabel11;
    private  JLabel jLabel12;
    private  JLabel jLabel13;
    private  JLabel jLabel14;
    private  JLabel jLabel15;
    private  JLabel jLabel16;
    private  JLabel jLabel17;
    private  JLabel jLabel18;
    private  JLabel jLabel19;
    private  JLabel jLabel2;
    private  JLabel jLabel3;
    private  JLabel jLabel4;
    private  JLabel jLabel5;
    private  JLabel jLabel6;
    private  JLabel jLabel7;
    private  JLabel jLabel8;
    private  JLabel jLabel9;
    private  JList<String> jList1;
    private  JList<String> jList2;
    private  JList<String> jList3;
    private  JList<String> jList4;
    private  JList<String> jList5;
    private  JList<String> jList6;
    private  JList<String> jList7;
    private  JList<String> jList8;
    private  JScrollPane jScrollPane1;
    private  JScrollPane jScrollPane2;
    private  JScrollPane jScrollPane3;
    private  JScrollPane jScrollPane4;
    private  JScrollPane jScrollPane5;
    private  JScrollPane jScrollPane6;
    private  JScrollPane jScrollPane7;
    private  JScrollPane jScrollPane8;
    private  JTextField jTextField1;
    private  JTextField jTextField10;
    private  JTextField jTextField2;
    private  JTextField jTextField3;
    private  JTextField jTextField4;
    private  JTextField jTextField5;
    private  JTextField jTextField6;
    private  JTextField jTextField7;
    private  JTextField jTextField8;
    private  JTextField jTextField9;
    private  JTextField jTextField11;
    private  JTextField jTextField12;
    private  JTextField jTextField13;
}

